package com.lly.trace

import android.app.Activity
import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import java.lang.ref.WeakReference
import java.util.Calendar


object CurrentPageTrace {
    const val TAG = "CurrentPageTrace"
    fun Any.name() = this::class.java.name
    val currentPage = hashMapOf<String, WeakReference<Fragment>>()
    var tracing = false
    private val activityLifecycleCallbacks = object :
        Application.ActivityLifecycleCallbacks {
        override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
            if (activity is FragmentActivity) {
                activity.supportFragmentManager.registerFragmentLifecycleCallbacks(object :
                    FragmentManager.FragmentLifecycleCallbacks() {
                    override fun onFragmentAttached(
                        fm: FragmentManager,
                        f: Fragment,
                        context: Context
                    ) {
                    }

                    override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
                        if (tracing) {
                            currentPage[f.name()] = WeakReference(f)
                        }
                    }

                    override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
                        currentPage.remove(f.name())
                    }
                }, true)

            }
        }

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

        }

        override fun onActivityStarted(activity: Activity) {
        }


        override fun onActivityResumed(activity: Activity) {
            if (tracing) {
                currentPage[activity.name()] = WeakReference(null)
            }
        }

        override fun onActivityPaused(activity: Activity) {
            currentPage.remove(activity.name())
        }

        override fun onActivityStopped(activity: Activity) {
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        }

        override fun onActivityDestroyed(activity: Activity) {
        }
    }

    private val broadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            Log.e(TAG, "time:${Calendar.getInstance().time}")
            currentPage.forEach { (k, v) ->
                val fragment = v.get()
                val isShow = fragment?.let { !it.isHidden } ?: true
                if (isShow) {
                    Log.e(TAG, k)
                } else {
                    Log.e(TAG, "$k :false")
                }
            }
        }

    }

    fun startListener(application: Application) {
        tracing = true
        currentPage.clear()
        application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
        application.registerReceiver(broadcastReceiver, IntentFilter("action.print.current.page"))
    }

    fun stopListener(application: Application) {
        tracing = false
        application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks)
        application.unregisterReceiver(broadcastReceiver)
        currentPage.clear()
    }


}